//
//  baseboard.m
//  警用舆情平台
//
//  Created by 魏强 on 15/5/15.
//  Copyright (c) 2015年 魏强. All rights reserved.
//

#import "KDMBaseBoard.h"
#import "KDMBaseBoardModel.h"
#import "RightPageBtn.h"
#import "KDDatePickerView.h"
#import "KDDateModel.h"
#import "KDTypeBtnBoard.h"
#import "KDTypeBtnBoardModel.h"
#import "KDRightPageBtnModel.h"
#import "UIView+Extension.h"
#import "KDSearchView.h"
#import "KDWebEditCellModel.h"
#import "SVProgressHUD.h"
#import "KDPickerSingle.h"
#define BEGIN_TIME @"beginTime"
#define END_TIME @"endTime"

@interface KDMBaseBoard ()<UITextFieldDelegate,KDDatePickerViewDelegate,KDTypeBtnBoardDelegate, UITextFieldDelegate>

@property (weak, nonatomic) IBOutlet RightPageBtn *customBtn;//“自定义”按钮
@property (nonatomic, retain) NSDateFormatter *fmt;
/***  装“信息范畴”板块的view */
@property (weak, nonatomic) IBOutlet UIView *firstView;
/***  装“地域”板块的View */
@property (weak, nonatomic) IBOutlet UIView *secondBoard;
/***  装“信息属性”板块的view */
@property (weak, nonatomic) IBOutlet UIView *thirdView;
@property (weak, nonatomic) IBOutlet UIView *datePickerView;//装时间选择的总View
/***  装“信息时间”板块的view */
@property (weak, nonatomic) IBOutlet UIView *fourthView;
/***  装“地域范围”板块的view */
@property (weak, nonatomic) IBOutlet UIView *fifthView;
/***  装“业务”板块的view */
@property (weak, nonatomic) IBOutlet UIView *businessView;

@property (weak, nonatomic) IBOutlet UIButton *beginBtn;

@property (weak, nonatomic) IBOutlet UIButton *endBtn;
@property (nonatomic, strong) KDDateModel *dateModel;
/***  时间板块 */
@property (nonatomic, strong) KDTypeBtnBoard *timeBoard;
/***  博客按钮 */
@property (weak, nonatomic) IBOutlet RightPageBtn *inlandBtn;
/***  信息类型板块的加载View的高度约束 */
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *secondBoard_Height;
/***  视频按钮 */
@property (weak, nonatomic) IBOutlet RightPageBtn *videoBtn;
/***  微博按钮 */
@property (weak, nonatomic) IBOutlet RightPageBtn *mediumBtn;
/***  信息类型板块 */
@property (weak, nonatomic)  KDTypeBtnBoard *sbusinesstypeBtnBoard;
/***  地域范围板块 */
@property (nonatomic, weak) KDTypeBtnBoard *regionBtnBoard;
/***  网站子板块板块 */
@property (nonatomic, weak) KDTypeBtnBoard *blockBtnBoard;
/***  datePickerView的高度约束 */
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *datePickerViewConstraint_Height;
/*** 确定按钮 */
@property (weak, nonatomic) IBOutlet UIButton *confirmBtn;
/***  view底层的scrollView */
@property (weak, nonatomic) IBOutlet UIScrollView *scrollView;
/***  scrollView的高度约束，其实是containsize */
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *scrollView_constraint_width;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *scrollView_constraint_height;

@property (weak, nonatomic) IBOutlet NSLayoutConstraint *firstView_constraint_height;

/**
 *  地域板块的高度约束
 */
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *fifthView_Constraint_Height;
/** *  信息属性板块的高度约束 */
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *thirdView_constraint_Height;

/** *  业务板块的高度约束 */
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *businessView_Constraint_Height;

/** *  那个label的顶端距离thirdView的顶端的高度 */
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *label_thirdView_Constraint_Top;

/*** 信息范畴页面被选中的按钮 */
@property (nonatomic, weak) RightPageBtn *scopeSelectBtn;
/*** 信息属性页面被选中的按钮 */
@property (nonatomic, weak) RightPageBtn *effectSelectBtn;
/*** 信息时间页面被选中的按钮 */
@property (nonatomic, weak) RightPageBtn *timeSelectBtn;
/*** 业务板块被选中的按钮 */
@property (nonatomic, weak) RightPageBtn *businessSelectBtn;
/*** 二次搜索的View */
@property (weak, nonatomic) IBOutlet KDSearchView *searchView;
/** *  中性按钮*/
@property (weak, nonatomic) IBOutlet RightPageBtn *neutralBtn;
/***  正面按钮 */
@property (weak, nonatomic) IBOutlet RightPageBtn *positiveBtn;
/***  当前是不是已经打开了侧边栏 */
@property (nonatomic, assign, getter=isOpen) BOOL open;
/** *  当下打开的DatePickerView */
@property (nonatomic, weak) KDDatePickerView *pickView;
/** *  选择子板块时显示子板块的textfield */
//@property (weak, nonatomic) UITextField *webBlockTf;

@end
@implementation KDMBaseBoard

#pragma mark -懒加载区


- (KDDateModel *)dateModel
{
    if (!_dateModel) {
        _dateModel = [[KDDateModel alloc]init];
    }
    return _dateModel;
}
- (NSDateFormatter *)fmt
{
    if (!_fmt) {
        _fmt = [[NSDateFormatter alloc]init];
        _fmt.locale = [[NSLocale alloc] initWithLocaleIdentifier:@"zh_CN"];;
        _fmt.dateFormat = @"yyyy-MM-dd";
    }
    return _fmt;
}
- (KDMBaseBoardModel *)model{
    if (!_model) {
        _model = [[KDMBaseBoardModel alloc]init];
    }
    return _model;
}
- (void)setWebBlockList:(NSArray *)webBlockList{
    _webBlockList = webBlockList;
    
}
# pragma mark - 初始化布局区

//初始化方法
+ (instancetype)baseboard
{
    KDMBaseBoard *board = [[[NSBundle mainBundle] loadNibNamed:@"KDMBaseBoard" owner:nil options:nil]lastObject];
    board.searchView.radius = 0.03;
    board.searchView.imageName = @"";

    board.searchView.SearchViewBackGroundColor = WQColor(46, 159, 56);

    board.confirmBtn.clipsToBounds = YES;
    //添加手势
    UITapGestureRecognizer *hide = [[UITapGestureRecognizer alloc] init];
    [hide addTarget:board action:@selector(hideTheKeyBoard)];
    [board addGestureRecognizer:hide];
    
    //设置开始按钮和结束按钮的边框风格
    //
    board.beginBtn.layer.borderWidth = 0.6f;
    board.beginBtn.layer.borderColor = WQColor(210 , 210, 210).CGColor;
    board.endBtn.layer.borderWidth = 0.6f;
    board.endBtn.layer.borderColor = WQColor(210 , 210, 210).CGColor;
    return board;
}
- (void)layoutSubviews
{
    [super layoutSubviews];
    
    if (self.x != screenW && self.open == NO) {
        self.open = YES;
    }
    if (self.x == screenW) {
        self.open = NO;
    }
    [self layoutIfNeeded];
}
- (void)dealloc
{
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}
/**
 *  开关的set方法
 */
- (void)setOpen:(BOOL)open{
    _open = open;
    if (open) {//说明正在打开
    [self setUpAll];
    }else{
        if (self.pickView) {
            [self.pickView hideDatePickerView];
        }
    }
}
- (void)drawRect:(CGRect)rect
{
    [super drawRect:rect];
//    KDLog(@"%@",NSStringFromCGRect(self.frame));
    self.scrollView_constraint_width.constant = self.width;
    [self layoutIfNeeded];
}
/**
 *  初始化所有
 */
- (void)setUpAll{
//    NSDictionary *dic = @{@"idstr":self.model.idstr};
    self.model = nil;
    if (!self.model.webSrcType) {
        self.model.webSrcType = @"0";
        //获取一周前的日期
        NSDate *newdate = [[NSDate date] initWithTimeInterval:- (24 *60 * 60 * 6 )sinceDate:[NSDate date]];
        self.model.startTime = [self.fmt stringFromDate:newdate];
        self.model.endTime = @"";
        self.model.timeIndex = @"9";
        self.model.docType = @"0";
        self.model.bussiness = @"0";
    }
    if (self.showReginType == 0) {
        self.model.regionType = 4;
    }else{
        self.model.regionType = self.showReginType;
    }
    NSDate *begin = [self.fmt dateFromString:self.model.startTime ];
    NSDate *end = [self.fmt dateFromString:self.model.endTime];
    if (begin) {
        self.dateModel.beginDate = begin;
    }else{
        self.dateModel.beginDate = [NSDate date];
    }
    if (end) {
        self.dateModel.endDate = end;
    }else{
        self.dateModel.endDate = [NSDate date];
    }
    [self refreshTitle];
    //给所有的rightBtn值
    self.scrollView.contentSize = (iOS7)?CGSizeMake(1, screenH -20 ):CGSizeMake(1, screenH - 49 );
    [self getAllData];
    [self setUpfirstView];
    [self setUpfifthView];
    [self setUpSecondView];
    [self setUpBusinessView];
//    //    [self setUpThirdView];
//    //找到被选中的btn
    
    self.scopeSelectBtn = (RightPageBtn *)[self.firstView viewWithTag:self.model.webSrcType.integerValue +1];
    self.effectSelectBtn = (RightPageBtn *)[self.thirdView viewWithTag:self.model.docType.integerValue + 6];
    self.timeSelectBtn = (RightPageBtn *)[self.fourthView viewWithTag:self.model.timeIndex.integerValue ];
    self.businessSelectBtn = (RightPageBtn *)[self.businessView viewWithTag:self.model.bussiness.integerValue +1];
    self.scopeSelectBtn.selected = YES;self.effectSelectBtn.selected = YES;
    self.timeSelectBtn.selected = YES;
    [self scopeViewBtnClick:self.scopeSelectBtn];
    [self effectBtnClick:self.effectSelectBtn];
    [self timeViewBtnClick:self.timeSelectBtn];
    [self businessBtnClick:self.businessSelectBtn];
    self.beginBtn.enabled = YES;
    self.endBtn.enabled = YES;
    //如果国内货媒体按钮被点击了，就展开
    //    if ([self.scopeSelectBtn.model.sign  isEqualToString: @"1"] || [self.scopeSelectBtn.model.sign isEqualToString:@"3"]) {//如果是国内或媒体按钮
    //        [self inlandBtnOrMediumBtnBtnClick];
    //    }
}


/**
 *  设置信息属性板块
 */
//- (void)setUpThirdView{
//    if ([self.mainPageType isEqualToString:@"预警"]) {//如果是预警板块，就隐藏掉这个板块
//        self.label_thirdView_Constraint_Top.constant = 0;
//        self.thirdView_constraint_Height.constant = 0;
//        self.thirdView.hidden = YES;
//    }
//}
/**
 *  设置类型板块
 */
-(void)setUpfirstView{
    if (self.mainPageType == 3 ||self.mainPageType ==1 ) {
        self.firstView_constraint_height.constant = 0;
        self.firstView.hidden = YES;
    }else{
        self.firstView_constraint_height.constant = 170;
        self.firstView.hidden = NO;
    }
}
/**
 *  设置搜索板块
 */
- (void)setUpfifthView{
    if (self.mainPageType == 2) {//返回为2（作者界面）则不需要显示搜索项
        self.fifthView_Constraint_Height.constant = 0;
//        self.regionBtnBoard.height = 0;
        self.fifthView.hidden = YES;
    }else {
        self.fifthView_Constraint_Height.constant = 50;
        self.fifthView.hidden = NO;
    }
    
}
/**
 *  设置地域板块
 */
- (void)setUpSecondView{
    if (self.mainPageType == 3  || self.mainPageType == 1) {//只有APP监测和网站监测要这个
        if (!self.regionBtnBoard) {
                KDTypeBtnBoard *regionBoard = [KDTypeBtnBoard typeBtnBoardWithFrame:CGRectMake(0, 0, self.width, 100)];
                regionBoard.delegate = self;
                self.regionBtnBoard = regionBoard;
                [self.secondBoard addSubview:regionBoard];
            }
        KDTypeBtnBoardModel *model = [[KDTypeBtnBoardModel alloc]init];
        CGFloat secondBoard_Height = (self.mainPageType == 3)? 100:200;
//        if (self.mainPageType == 3){//如果是APP监测，就显示地域选项
            model.labelText = @"地域范围";
            self.regionBtnBoard.hidden = NO;
            if (self.showReginType == 0) {//返回为0则不需要显示筛选项
                secondBoard_Height = secondBoard_Height - 100;
//                self.secondBoard_Height.constant = secondBoard_Height;
                self.regionBtnBoard.height = 0;
                self.regionBtnBoard.hidden = YES;
//                return;
            }else if (self.showReginType == 1){//返回1时 筛选项为 辖区，本市 ，本省，全国
                
                model.btnTitleArr = @[@"辖区", @"本市", @"本省", @"全国"];
                model.btnTagArr = @[@(101), @(102), @(103), @(104)];
                model.selectBtnTag = self.model.regionType +100;
                self.regionBtnBoard.model = model;
                
            }else if (self.showReginType == 2){//返回2时 筛选项为 辖区，本省，全国
                model.btnTitleArr = @[@"本市", @"本省", @"全国"];
                model.btnTagArr = @[@(102), @(103), @(104)];
                model.selectBtnTag = self.model.regionType +100;
                self.regionBtnBoard.model = model;
            }else if (self.showReginType == 3){//返回1时 筛选项为 辖区，全国
                model.btnTitleArr = @[@"本省", @"全国"];
                model.btnTagArr = @[@(103), @(104)];
                model.selectBtnTag = self.model.regionType +100;
                self.regionBtnBoard.model = model;
            }
        self.secondBoard_Height.constant = secondBoard_Height;

        if (self.mainPageType == 1){//如果是网站监测那么就在加上子板块
            KDTypeBtnBoardModel *model1 = [[KDTypeBtnBoardModel alloc]init];
            if (!self.blockBtnBoard) {
                KDTypeBtnBoard *blockBtnBoard = [KDTypeBtnBoard typeBtnBoardWithFrame:CGRectMake(0, CGRectGetMaxY(self.regionBtnBoard.frame), self.width, 100)];
                blockBtnBoard.delegate = self;
                self.blockBtnBoard = blockBtnBoard;
                [self.secondBoard addSubview:blockBtnBoard];
            }
            if (self.webBlockList.count == 0) {//没有子板块
                self.secondBoard_Height.constant = secondBoard_Height - 100;
                self.blockBtnBoard.height = 0;
                self.blockBtnBoard.hidden = YES;
                return;
            }else{
                NSMutableArray *titleArr = [NSMutableArray array];
                NSMutableArray *tagArr = [NSMutableArray array];
                for (KDWebEditCellModel *model in self.webBlockList) {
                    [titleArr addObject:model.w_Name];
                    [tagArr addObject:@(model.w_Id)];
                }
                model1.labelText = @"版块";
                self.secondBoard_Height.constant = secondBoard_Height;
                model1.btnTitleArr = titleArr;
                model1.btnTagArr = tagArr;
                model1.selectBtnTag = [tagArr[0] integerValue];
                self.blockBtnBoard.hidden = NO;
                self.blockBtnBoard.height = 100;
                self.blockBtnBoard.model = model1;
            }
            
        }
        [self layoutIfNeeded];
      
    }else {
        self.secondBoard_Height.constant = 0;
        self.secondBoard.hidden = YES;
    }
    
    
}
- (void)setUpBusinessView{
    if (self.mainPageType == 1 || self.mainPageType == 3) {//如果是网站板块或APP板块就显示
        self.businessView_Constraint_Height.constant = 100;
        self.businessView.hidden = NO;
    }else {
        self.businessView_Constraint_Height.constant = 0;
        self.businessView.hidden = YES;

    }
}

//取得所有btn的模型的数据
- (void) getAllData
{
    for (RightPageBtn *btn in self.firstView.subviews) {//信息类型
        if ([btn isKindOfClass:[RightPageBtn class]]) {
            KDRightPageBtnModel *model = [[KDRightPageBtnModel alloc]init];
            //请求需要的参数
            model.type = @"webSrcType";
            model.sign = [NSString stringWithFormat:@"%ld",(long)btn.tag -1];
            btn.model = model;
        }
    }
    //信息属性正反面
    for (RightPageBtn *btn in self.thirdView.subviews) {
        if (btn.tag >= 6 && btn.tag <= 9 &&[btn isKindOfClass:[RightPageBtn class]] ) {
            if (!btn.model) {
                KDRightPageBtnModel *model = [[KDRightPageBtnModel alloc]init];
                model.type = @"docType";
                model.sign = [NSString stringWithFormat:@"%ld",(long)btn.tag -6];
                btn.model = model;
            }
        }
    }
    //信息业务
    for (RightPageBtn *btn in self.businessView.subviews) {
        if (btn.tag >= 1 && btn.tag <= 2 &&[btn isKindOfClass:[RightPageBtn class]] ) {
            if (!btn.model) {
                KDRightPageBtnModel *model = [[KDRightPageBtnModel alloc]init];
                model.type = @"business";
                model.sign = [NSString stringWithFormat:@"%ld",(long)btn.tag -1];
                btn.model = model;
            }
        }
    }
    //信息时间
    for (RightPageBtn *btn in self.fourthView.subviews) {
        if (btn.tag >= 9 &&[btn isKindOfClass:[RightPageBtn class]]) {
            if (!btn.model) {
                KDRightPageBtnModel *model = [[KDRightPageBtnModel alloc]init];
                model.type = @"startTime";
                model.sign = [NSString stringWithFormat:@"%ld",(long)btn.tag];
                btn.model = model;
            }
        }
    }
}


#pragma mark - 事件点击区
/**
 *  信息范畴板块的按钮被点击
 *
 *  @param sender 被点击的按钮
 */
- (IBAction)scopeViewBtnClick:(RightPageBtn *)sender {
    //    KDLog(@"%ld",(long)sender.tag);
    //在信息范畴中遍历选择
    for (RightPageBtn *btn in self.firstView.subviews)
    {
        if ([btn isKindOfClass:[RightPageBtn class]]) {
            if (btn.selected) {//找出之前选中的那个
                btn.selected = NO;
                //                break;
            }
        }
    }
    sender.selected = YES;
    //特殊情况：当国内按钮或媒体按钮被点击了，进入这个方法
    self.model.webSrcType = sender.model.sign;
    //    [self inlandBtnOrMediumBtnBtnClick];
}
/**
 *  信息属性板块的按钮被点击了
 *
 *  @param sender 被点击的按钮
 */
- (IBAction)effectBtnClick:(RightPageBtn *)sender {
    for (RightPageBtn *btn in self.thirdView.subviews){
        if ([btn isKindOfClass:[UIButton class]]) {
            if (btn.selected) {
                btn.selected = NO;
                //                break;
            }
        }
    }
    sender.selected = YES;
    self.model.docType = sender.model.sign;
}
- (IBAction)businessBtnClick:(RightPageBtn *)sender{
    for (RightPageBtn *btn in self.businessView.subviews){
        if ([btn isKindOfClass:[UIButton class]]) {
            if (btn.selected) {
                btn.selected = NO;
                //                break;
            }
        }
    }
    sender.selected = YES;
    self.model.bussiness = sender.model.sign;
}
/**
 *  信息时间板块的按钮被点击了
 *
 *  @param sender 被点击的按钮
 */
- (IBAction)timeViewBtnClick:(id)sender{
    
    RightPageBtn *rightBtn = (RightPageBtn *)sender;
    KDLog(@"%ld", rightBtn.tag);
    {for (RightPageBtn *btn in self.fourthView.subviews)
    {
        if ([btn isKindOfClass:[RightPageBtn class]]) {
            if (btn.selected) {
                btn.selected = NO;
            }
        }
    }
    }
    rightBtn.selected = YES;
    self.model.startTime = rightBtn.model.startTime;
    self.model.endTime = rightBtn.model.endTime;
    self.model.timeIndex = rightBtn.model.sign;
    if (self.customBtn.selected) {//如果自定义按钮被选中，那么就打开日期选择器
        [self openDatePicker];
    }
    else{//如果没有被选中，就关闭日期选择器
        [self removeDatePicker];
    }
}

//显示日期选择
- (void)openDatePicker
{
    
    self.datePickerView.hidden = NO;
    [UIView animateWithDuration:0.3f animations:^{
        self.datePickerView.alpha = 1.0f;
        self.datePickerViewConstraint_Height.constant = 40;
//        [self layoutIfNeeded];
    }];
    NSDate *curDate = [NSDate date];
    self.curDate = curDate;
    //开始时间
    NSString *begin = [self.fmt stringFromDate:self.dateModel.beginDate];
    //结束时间
    NSString *end = [self.fmt stringFromDate:self.dateModel.endDate];
    self.model.startTime = begin;
    self.model.endTime = end;
    self.scrollView.contentSize = (iOS7) ? CGSizeMake(1, self.scrollView.contentSize.height + 20): CGSizeMake(1, screenH - 49 +40);
    
//    KDLog(@"%@",NSStringFromCGSize(self.scrollView.contentSize));
    [self refreshTitle];
}
//隐藏日期选择
- (void)removeDatePicker
{
    [UIView animateWithDuration:0.3f animations:^{
        self.datePickerViewConstraint_Height.constant = 0;
//        [self layoutIfNeeded];
        self.datePickerView.alpha = 0;
        
    }completion:^(BOOL finished) {
        self.datePickerView.hidden = YES;
        self.scrollView.contentSize =  (iOS7) ? CGSizeMake(1, screenH + 20):  CGSizeMake(1, CGRectGetMaxY(self.confirmBtn.frame)+30);
//        KDLog(@"%@",NSStringFromCGSize(self.scrollView.contentSize));

    }];
}
/**
 *  开始时间按钮被点击了
 *
 */
- (IBAction)beginBtnClick:(id)sender {
    [self showPickViewWithType:KDDatePickerTypeBegin];

    
}

//结束时间按钮被点击了
- (IBAction)endBtnClick:(id)sender {
    [self showPickViewWithType:KDDatePickerTypeEnd];

}
/**
 *  type：0:begin；1:end
 */
- (void)showPickViewWithType:(NSInteger)type{
    KDDatePickerView *pickView;
    if (type) {
        pickView = [KDDatePickerView showWithType:type andDate:self.dateModel.endDate];
    }else{
        pickView = [KDDatePickerView showWithType:type andDate:self.dateModel.beginDate];
    }
    
    pickView.delegate = self;
    
}
/**
 *  确定按钮被点击了
 */
- (IBAction)confirmBtnClick:(RightPageBtn *)sender {
    self.model.secondSearch = self.searchView.text;
    KDMBaseBoardModel *model = self.model;
    if (_delegate &&[ _delegate respondsToSelector:@selector( baseBoard:confirmBtnDidClickWithBaseBoardModel:)]) {
        [_delegate baseBoard:self confirmBtnDidClickWithBaseBoardModel:model];
    }
}

#pragma mark - 刷新字体
- (void)refreshTitle
{
    //开始时间
    NSString *begin = [self.fmt stringFromDate:self.dateModel.beginDate];
    //结束时间
    NSString *end = [self.fmt stringFromDate:self.dateModel.endDate];
    [self.beginBtn setTitle:begin forState:UIControlStateNormal];
    [self.endBtn setTitle:end forState:UIControlStateNormal];
}

#pragma mark - KDDatePickerViewDelegate
/**
 *  datepicker的设置按钮被点击了
 *
 *  @param pickerView 时间选择器
 *  @param date       被选中的时间
 *  @param type      确定是开始时间还是结束时间
 */
- (void)datePickerView:(KDDatePickerView *)pickerView settingBtnClickWithDate:(NSDate *)date andType:(KDDatePickerType)type
{
    NSComparisonResult result1 = [[NSDate date] compare:date];
    if (result1 == -1) {
        [SVProgressHUD setOffsetFromCenter:UIOffsetMake(self.centerX - screenW *0.5, 0)];
        [SVProgressHUD showInfoWithStatus:@"选取时间应小于或等于今天！" maskType:SVProgressHUDMaskTypeGradient];
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            if (type != KDDatePickerTypeBegin) {
                [self endBtnClick:self.endBtn];
            }else{
                [self beginBtnClick:self.beginBtn];
            }
        });
        return;
    }
    if (type == KDDatePickerTypeBegin) {//如果是开始时间被设置了
        //比较开始时间和结束时间，看看哪个大
        NSComparisonResult result = [date compare:self.dateModel.endDate];
        if (result != - 1) {//开始时间大于结束时间
            [SVProgressHUD setOffsetFromCenter:UIOffsetMake(self.centerX - screenW *0.5, 0)];
            [SVProgressHUD showInfoWithStatus:@"开始时间应小于结束时间！" maskType:SVProgressHUDMaskTypeGradient];
            dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                [self endBtnClick:self.endBtn];
            });
            return;
        }else{//开始时间小于结束时间
            self.dateModel.beginDate = date;
        }
    }
    else{
        NSComparisonResult result = [date compare:self.dateModel.beginDate];
        if (result == -1) {//开始时间大于结束时间
            [SVProgressHUD setOffsetFromCenter:UIOffsetMake(self.centerX - screenW *0.5, 0)];
            [SVProgressHUD showInfoWithStatus:@"开始时间应小于结束时间！" maskType:SVProgressHUDMaskTypeGradient];
            dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                [self endBtnClick:self.endBtn];
            });
            return;
        }else{//开始时间小于结束时间
            self.dateModel.endDate = date;
        }
    }
    self.model.beginDate = self.dateModel.beginDate;
    self.model.startTime = [self.fmt stringFromDate:self.dateModel.beginDate];
    self.model.endTime = [self.fmt stringFromDate:self.dateModel.endDate];
    self.model.endDate = self.dateModel.endDate;
    [self refreshTitle];
}

-(void)cancelBtnClick{
    self.beginBtn.enabled = YES;
    self.endBtn.enabled = YES;
}
#pragma mark - KDTypeBtnBoardDelegate

- (void)typeBtnBoard:(KDTypeBtnBoard *)typeBtnBoard BtnClickedWithTag:(NSInteger)tag andLabelTitle:(NSString *)tableTitle{
//
    if ([tableTitle isEqualToString:@"地域范围"]) {
        self.model.regionType = tag -100;;
    }else if([tableTitle isEqualToString:@"子板块选择"]){
        self.model.pageId = [NSString stringWithFormat:@"%ld", tag];
    }
}

#pragma mark - UITextFieldDelegate
- (void)textFieldDidBeginEditing:(UITextField *)textField{
    [textField resignFirstResponder];
    //不要了，以后有需要再用你
//    KDPickerSingle *pickerSingle = [[KDPickerSingle alloc]init];
////    NSMutableArray *arr = [NSMutableArray arrayWithArray:self.webBlockList];
//    NSMutableArray *arrayData = [NSMutableArray array];
//    for (int i = 1; i < 1000; i++) {
//        NSString *string = [NSString stringWithFormat:@"%d", i];
//        [arrayData addObject:string];
//    }
//    pickerSingle.arrayData = arrayData;
//    pickerSingle.title = @"请选择子板块";
//    pickerSingle.titleUnit = @"板块";
//    pickerSingle.contentMode = STPickerContentModeBottom;
//    pickerSingle.delegate = self;
//    [pickerSingle show];
    
    
}

#pragma mark - STPickerSingleDelegate
//- (void)pickerSingle:(KDPickerSingle *)pickerSingle selectedTitle:(NSString *)selectedTitle{
//    self.webBlockTf.text = selectedTitle;
//}
#pragma mark - 通知接收区

#pragma mark - 隐藏键盘
- (void)hideTheKeyBoard
{
    [self endEditing:YES];
}
@end
